#include <linux/config.h>
#include <linux/delay.h>

#if defined(MODULE)
	#include <linux/module.h>
#endif

#include <linux/videodev.h>
#include <linux/video_decoder.h>
#include <linux/version.h>
#include <asm/io.h>
#include "fcap_ctrl.h"
#include "gpio.h"
#include "video_type.h"

#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0))
	#include <asm/arch/platform/spec.h>
	#define CPE_CAPTURE_VA_BASE CAP_FTCAP010_VA_BASE
	#define CPE_GPIO_VA_BASE GPIO_FTGPIO010_VA_BASE
	#define CPE_CAPTURE_BASE CAP_FTCAP010_PA_BASE
#else
	#include <asm/arch/cpe/cpe.h>
	#include <asm/arch/cpe_int.h>		
#endif
//#include "firmware_7113.c"

// fcap_8120_reset_workaround, 8120 only, we capture 352x240 and 176x120, the left-up of the 
// image appear a incorrect green pixel, so we reset the capture IP before we trigger
//static int fcap_8120_reset_workaround; 

static int fcap_reset_workaround; 

void FC_reset(void)
{
	volatile CAP 	*capture = CPE_CAPTURE_VA_BASE;
	capture->Control |= CTRL_RST;
#if 1
	// clear all interrupt
	capture->Control = CTRL_RST;
	capture->Format  = 0;
#ifdef CONFIG_CCIR656	
#ifdef VIDEO_MODE_422
	capture->Format = IN_FMT_CCIR656_VGA | OUT_FMT_422 | OUT_SIZE_FULL;	
#else
	capture->Format = IN_FMT_CCIR656_VGA | OUT_FMT_420 | OUT_SIZE_FULL;
#endif	
#else
#ifdef VIDEO_MODE_422
	capture->Format = IN_FMT_CCIR601_VGA | OUT_FMT_422 | OUT_SIZE_FULL;	
#else
	capture->Format = IN_FMT_CCIR601_VGA | OUT_FMT_420 | OUT_SIZE_FULL;
#endif
#endif

#endif
    capture->Status  = 0;


}


void FC_reset_hw(void)
{
	volatile CAP 	*capture = CPE_CAPTURE_VA_BASE;
	capture->Control |= CTRL_RST;
#if 0//Francis
	// clear all interrupt
//	capture->Control = CTRL_RST;
	capture->Format  = 0;
#ifdef CONFIG_CCIR656	
#ifdef VIDEO_MODE_422
	capture->Format = IN_FMT_CCIR656_VGA | OUT_FMT_422 | OUT_SIZE_FULL;	
#else
	capture->Format = IN_FMT_CCIR656_VGA | OUT_FMT_420 | OUT_SIZE_FULL;
#endif	
#else
#ifdef VIDEO_MODE_422
	capture->Format = IN_FMT_CCIR601_VGA | OUT_FMT_422 | OUT_SIZE_FULL;	
#else
	capture->Format = IN_FMT_CCIR601_VGA | OUT_FMT_420 | OUT_SIZE_FULL;
#endif
#endif

#endif
    capture->Status  = 0;
}

// GPIO
void FC_set_seq(__u32 seq)
{
	volatile CAP 	*capture = CPE_CAPTURE_VA_BASE;
	switch( seq) {
		case 0: capture->Format |= DATAFMT_2D; break;
		case 1: capture->Format &= ~DATAFMT_2D; break;
		case 2: capture->Format |= DATAFMT_2D; break;
		default: break;
	}
}


int FC_test_capture(ulong y, ulong u, ulong v)
{
	volatile CAP 	*capture = CPE_CAPTURE_VA_BASE;
	int 			i=0, ret= 1;

	mdelay(100);
	/* trigger capture */
	capture->Control |= CTRL_RST;
 	capture->Ybase = y;
	capture->Ubase = u;
	capture->Vbase = v;
    capture->Status = 0;
	capture->Control |= (CTRL_INTACK | CTRL_PENB | CTRL_CENB);
	
	#ifdef FIE8150
	capture->Framecnt = fcnt<<8 | fthreoshold<<16;
	capture->Framedec = 0x01;
	#endif
		
	while((capture->Status & 0x01)==0) {
		set_current_state(TASK_INTERRUPTIBLE);
		schedule_timeout (1);
		if (++i > 300) {  /* configue timeout */
			ret= -1;
			break;
		}
	}
	return ret;
}

void FC_check_intSt(void)
{
	volatile CAP 	*capture = CPE_CAPTURE_VA_BASE;
	unsigned int 	interrupt_status;
	
 	interrupt_status = capture->Status;
    capture->Status = 0;
	
 	if((interrupt_status & 0x01)==0)
		printk("Interrupt Handler has received an incorrect INT !\n");
 	/* check the frame is OK */
	if (interrupt_status & 0x4)
		printk("Fcap: AHB bus error!!\n");
	if (interrupt_status & 0x2){
		printk("Fcap: fifo overflow!!\n");

	}
}

void FC_trigger_interupt(ulong y, ulong u, ulong v)
{
	volatile CAP 	*capture = CPE_CAPTURE_VA_BASE;
	
	#if defined( FIE8150 ) || defined ( FIC8120 )		
	if(fcap_reset_workaround == 1)
		capture->Control |= CTRL_RST;
	#endif		
    	capture->Status = 0;
 	capture->Ybase = y;
	capture->Ubase = u;
	capture->Vbase = v;
	capture->Control |= (CTRL_INTACK | CTRL_PENB | CTRL_CENB |
					   	 CTRL_TEINT | CTRL_FTINT);
					   	 
	#ifdef FIE8150
	capture->Framecnt = fcnt<<8 | fthreoshold<<16;
	capture->Framedec = 0x01;
	#endif
}


#ifdef FIE8150
void GPIO_Video_In_Select(int mode)
{
	volatile CAP 	*capture = CPE_CAPTURE_VA_BASE;
	outl(inw(CPE_GPIO_VA_BASE + GPIO_PINOUT_OFFSET) | BIT1,
						CPE_GPIO_VA_BASE + GPIO_PINOUT_OFFSET);
	
	switch( mode ) {
		case 0:	// CMOS_sensor -> M100
			outl(BIT1, CPE_GPIO_VA_BASE + GPIO_DATACLR);
			capture->Control &= ~CTRL_YSEQ_ALL;
			break;
		case 1: 	// CMOS_sensor -> DeInterlace -> M100
			outl(BIT1, CPE_GPIO_VA_BASE + GPIO_DATACLR);
			capture->Control  |= CTRL_YSEQ_EVEN;
			break;
		case 2: // Ex_TV_Decoder -> M100
		case 3:// Ex_TV_Decoder -> M100
			break;
		case 4:	// 7113 -> M100
			outl(BIT1, CPE_GPIO_VA_BASE + GPIO_DATASET);
			capture->Control &= CTRL_YSEQ_ALL;
			break;
		case 5:	// 7113 -> DeInterlace -> M100
			outl(BIT1, CPE_GPIO_VA_BASE + GPIO_DATASET);
			capture->Control |= ~CTRL_YSEQ_EVEN;
			break;
		case 6:	// 7113 -> M100 (no DeInterlace)  720 x 240
		    	outl(BIT1, CPE_GPIO_VA_BASE + GPIO_DATASET);

			capture->Control &= CTRL_YSEQ_ALL;
			break;
		default:
			break;
	}
}
#elif defined (FIC8120)
void GPIO_Video_In_Select(int mode)
{

	volatile CAP 	*capture = CPE_CAPTURE_VA_BASE;
	// set GPIO 12 as output
    	outl(inw(CPE_GPIO_VA_BASE + GPIO_PINOUT_OFFSET) | BIT12,
							  CPE_GPIO_VA_BASE + GPIO_PINOUT_OFFSET);
	switch (mode) {
		case 0:	// CMOS_sensor -> M100
			outl(BIT12, CPE_GPIO_VA_BASE + GPIO_DATACLR);
			capture->Control &= ~CTRL_YSEQ_ALL;
			break;
		case 1:	// CMOS_sensor -> DeInterlace -> M100
			outl(BIT12, CPE_GPIO_VA_BASE + GPIO_DATACLR);
			capture->Control |= CTRL_YSEQ_EVEN;
			break;
		case 2:	// Ex_TV_Decoder -> M100
			printk("\nfcap: still debugging!!\n");
			break;
		case 3:	// Ex_TV_Decoder -> DeInterlace -> M100
			printk("\nfcap: still debugging!!\n");
			break;
		case 4:	// 7113 -> M100
		    	outl(BIT12, CPE_GPIO_VA_BASE + GPIO_DATASET);
			capture->Control &= ~CTRL_YSEQ_ALL;
			break;
		case 5:	// 7113 -> DeInterlace -> M100
		    	outl(BIT12, CPE_GPIO_VA_BASE + GPIO_DATASET);
			capture->Control |= CTRL_YSEQ_EVEN;
			break;
		case 6:	// 7113 -> M100 (no DeInterlace)  720 x 240
		    	outl(BIT12, CPE_GPIO_VA_BASE + GPIO_DATASET);
			capture->Control &= ~CTRL_YSEQ_ALL;
			//capture->High = 720;
			//capture->Times = 720*240*1.5/16;
			break;			
		default:
			break;
	}
}

#endif

//static inline int fcap_set_frame_size(__u32 width, __u32 height)
int FC_set_frame_size(int video_type,__u32 width, __u32 height)
{
//	volatile CAP 	*capture = cap.capture;
	volatile CAP 	*capture = CPE_CAPTURE_VA_BASE;
	int format = capture->Format & ~(OUT_SIZE_MASK | IN_FMT_MASK | BIT9);
	int high, times;

    //printk("FC_set_frame_size=%dx%d video_type=%d\n",width,height,video_type);

    if(video_type==-1) //auto select
    {
    	if (width == 640 && height == 480) {
            GPIO_Video_In_Select(0);
    		format &= ~CTRL_YUVIFMT;
    //		format |= (OUT_SIZE_FULL | IN_FMT_CCIR656_VGA | BIT9);
    		format |= (OUT_SIZE_FULL | IN_FMT_CCIR656_VGA);
    		high = 640;
    		times = 640*480*BYTES/16;
    #if defined( FIC8120 ) || defined (FIE8150)
    		fcap_reset_workaround = 0;
    #endif
    	}
    #ifdef million_pixel
    	else if (width == 1280 && height == 1024) {
    	    GPIO_Video_In_Select(0);
    		format &= ~CTRL_YUVIFMT;
    		format |= (OUT_SIZE_FULL | IN_FMT_CCIR656_VGA | BIT9);
    //		format |= (OUT_SIZE_FULL | IN_FMT_CCIR656_VGA);
    		high = 1280;
    		times = 1280*1024*BYTES/16;
    
#if defined( FIC8120 ) || defined (FIE8150)
    		fcap_reset_workaround = 0;
#endif
    	}
    #endif
    	else if (width == 320 && height == 240) {
            GPIO_Video_In_Select(0);
    		format &= ~CTRL_YUVIFMT;
    //		format |= (OUT_SIZE_CIF | IN_FMT_CCIR656_VGA | BIT9);
    		format |= (OUT_SIZE_CIF | IN_FMT_CCIR656_VGA );
    		high = 320;
    		times = 320*240*BYTES/16;
#if defined( FIC8120 ) || defined (FIE8150)	
    		fcap_reset_workaround = 0;
#endif
    	}
    	else if ((width == 160 && height == 128) || (width == 160 && height == 120)) {
    	    	GPIO_Video_In_Select(0);
    		format &= ~CTRL_YUVIFMT;
    //		format |= (OUT_SIZE_QCIF | IN_FMT_CCIR656_VGA | BIT9);
    		format |= (OUT_SIZE_QCIF | IN_FMT_CCIR656_VGA );
    		high = 160;
    		times = 160*120*BYTES/16;
    		#if defined( FIC8120 ) || defined (FIE8150)	
    		fcap_reset_workaround = 0;
    		#endif
    	}
    	else if (width == 720 && height == 576) {
    	    	GPIO_Video_In_Select(5);
    //		format |= (OUT_SIZE_FULL | CTRL_YUVIFMT | IN_FMT_CCIR601_NTSC | BIT9);
    		format |= (OUT_SIZE_FULL | CTRL_YUVIFMT | IN_FMT_CCIR601_NTSC);
    		high = 720;
    		times = 720*576*BYTES/16;
    		#if defined( FIC8120 ) || defined (FIE8150)	
    		fcap_reset_workaround = 0;
    		#endif
    	}
    	else if (width == 720 && height == 480) {
    	    	GPIO_Video_In_Select(5);
    //		format |= (OUT_SIZE_FULL | CTRL_YUVIFMT | IN_FMT_CCIR601_NTSC | BIT9);
    		format |= (OUT_SIZE_FULL | CTRL_YUVIFMT | IN_FMT_CCIR601_NTSC );

    		high = 720;
    		times = 720*480*BYTES/16;
    		#if defined( FIC8120 ) || defined (FIE8150)	
    		fcap_reset_workaround = 0;
    		#endif
    	}
    	else if (width == 720 && height == 240) {
    	    	GPIO_Video_In_Select(6);
    		format |= (OUT_SIZE_FULL | IN_FMT_CCIR656_NTSC | (0x1 << 9)) ;
    		high = 720;
    		times = 720*240*BYTES/16;
    		#if defined( FIC8120 ) || defined (FIE8150)	
    		fcap_reset_workaround = 0;
    		#endif
    	}	
    	else if (width == 352 && height == 240) {
    	    	GPIO_Video_In_Select(4);
    		format &= ~CTRL_YUVIFMT;
    //		format |= (OUT_SIZE_CIF | IN_FMT_CCIR656_NTSC | BIT9);
    		format |= (OUT_SIZE_CIF | IN_FMT_CCIR656_NTSC);
    		high = 352;
    		times = 352*240*BYTES/16;
    		#if defined( FIC8120 ) || defined (FIE8150)	
    		fcap_reset_workaround = 1;
    		#endif
    	}
    	else if ((width == 176 && height == 128) || (width == 176 && height == 120)) {
    	    	GPIO_Video_In_Select(4);
    		format &= ~CTRL_YUVIFMT;
    //		format |= (OUT_SIZE_QCIF | IN_FMT_CCIR656_NTSC | BIT9);
    		format |= (OUT_SIZE_QCIF | IN_FMT_CCIR656_NTSC );
    		high = 176;
    		times = 176*120*BYTES/16;
    		#if defined( FIC8120 ) || defined (FIE8150)	
    		fcap_reset_workaround = 1;
    		#endif
    	}
    	else if (width == 352 && height == 288) {
    	    	GPIO_Video_In_Select(4);
    		format &= ~CTRL_YUVIFMT;
    //		format |= (OUT_SIZE_CIF | IN_FMT_CCIR656_PAL | BIT9);
    		format |= (OUT_SIZE_CIF | IN_FMT_CCIR656_PAL);
    		high = 352;
    		times = 352*288*BYTES/16;
    		#if defined( FIC8120 ) || defined (FIE8150)	
    		fcap_reset_workaround = 1;
    		#endif
    	}
    	else if (width == 176 && height == 144) {
    	    	GPIO_Video_In_Select(4);
    		format &= ~CTRL_YUVIFMT;
    //		format |= (OUT_SIZE_QCIF | IN_FMT_CCIR656_PAL | BIT9);
    		format |= (OUT_SIZE_QCIF | IN_FMT_CCIR656_PAL);
    		high = 176;
    		times = 176*144*BYTES/16;
    		#if defined( FIC8120 ) || defined (FIE8150)	
    		fcap_reset_workaround = 1;
    		#endif
    	}	
    	else {
    		return -1;	// failure
    	}
    }
    else
    {
        switch (video_type)
        {
            case INPUT_OV7640:
            case INPUT_OV7660:
            case INPUT_OV7710:
            case INPUT_OV7720:
            case INPUT_MT9V11:
                if (width == 640 && height == 480) 
                {
                    GPIO_Video_In_Select(0);
                    format &= ~CTRL_YUVIFMT;
                    if (video_type == INPUT_OV7720)
                      format |= (OUT_SIZE_FULL | IN_FMT_CCIR656_VGA | BIT9);    //steven 7720
                    else
                      format |= (OUT_SIZE_FULL | IN_FMT_CCIR656_VGA);
                    high = 640;
                    times = 640*480*BYTES/16;
#if defined( FIC8120 ) || defined (FIE8150)
                    fcap_reset_workaround = 0;
#endif
                }
                else if (width == 320 && height == 240) 
                {
                    GPIO_Video_In_Select(0);
                    format &= ~CTRL_YUVIFMT;
                    if (video_type == INPUT_OV7720){
                      format |= (OUT_SIZE_CIF | IN_FMT_CCIR656_VGA | BIT9 );    //steven 7720
                      high = 640;
                    }
                    else{
                      format |= (OUT_SIZE_CIF | IN_FMT_CCIR656_VGA );
                      high = 320;
                    }
                    times = 320*240*BYTES/16;
#if defined( FIC8120 ) || defined (FIE8150)	
                    fcap_reset_workaround = 0;
#endif
                }
                else if ((width == 160) && ((height == 120) || (height == 128)))
                {
                    GPIO_Video_In_Select(0);
                    format &= ~CTRL_YUVIFMT;
                    if (video_type == INPUT_OV7720){
                      format |= (OUT_SIZE_QCIF | IN_FMT_CCIR656_VGA | BIT9 );    //steven 7720
                      high = 640;
                    }
                    else{
                      format |= (OUT_SIZE_QCIF | IN_FMT_CCIR656_VGA );
                      high = 160;
                    }
                    times = 160*120*BYTES/16;
#if defined( FIC8120 ) || defined (FIE8150)	
                    fcap_reset_workaround = 0;
#endif
                }
                break;
            case INPUT_MT9M112: 
                GPIO_Video_In_Select(0);
                format &= ~CTRL_YUVIFMT;                
                high = 640;
                if(width == 640 && height == 480)
                {
                    format |= (OUT_SIZE_FULL | IN_FMT_CCIR656_VGA | BIT9);
    	            times = 640*480*BYTES/16;
    	        }
    	        else if (width == 320 && height == 240) 
                {
                    format |= (OUT_SIZE_CIF | IN_FMT_CCIR656_VGA | BIT9);                    	        
    	            times = 320*240*BYTES/16;
    	        }
                else if ((width == 160) && ((height == 120) || (height == 128)))
                {  
                    format |= (OUT_SIZE_QCIF | IN_FMT_CCIR656_VGA | BIT9);                                 
    	            times = 160*120*BYTES/16;  
    	        }                	        	        
                else if(width==1280)
                {
                    format |= (OUT_SIZE_FULL | IN_FMT_CCIR656_VGA | BIT9);                
                    high = 1280;
                    times = 1280*1024*BYTES/16;
                }
                else
                    return -1;
                    
#if defined( FIC8120 ) || defined (FIE8150)
                fcap_reset_workaround = 0;
#endif
                break;            
            case INPUT_OV9655:            
                GPIO_Video_In_Select(0);
                format &= ~CTRL_YUVIFMT;
                format |= (OUT_SIZE_FULL | IN_FMT_CCIR656_VGA | BIT9);
                if(width==640)
                {
    	            high = 640;
    	            times = 640*480*BYTES/16;
    	        }
                else if(width==1280)
                {
                    high = 1280;
                    times = 1280*1024*BYTES/16;
                }
                else
                    return -1;
                    
#if defined( FIC8120 ) || defined (FIE8150)
                fcap_reset_workaround = 0;
#endif
                break;
            case INPUT_SONY_ICX408AK:
                if(width == 720 && height == 480)
                {
           	        GPIO_Video_In_Select(1);
        		    format |= (OUT_SIZE_FULL | CTRL_YUVIFMT | IN_FMT_CCIR601_NTSC );
        		    high = 720;
        		    times = 720*480*BYTES/16;
#if defined( FIC8120 ) || defined (FIE8150)	
        		    fcap_reset_workaround = 0;
#endif
                }
                else if(width == 352 && height == 240)
                {
           	    	GPIO_Video_In_Select(0);
            		format &= ~CTRL_YUVIFMT;
            		format |= (OUT_SIZE_CIF | IN_FMT_CCIR656_NTSC);
            		high = 352;
            		times = 352*240*BYTES/16;
#if defined( FIC8120 ) || defined (FIE8150)	
            		fcap_reset_workaround = 1;
#endif
                }
                else if(width == 176 && height == 128)
                {
        	    	GPIO_Video_In_Select(0);
            		format &= ~CTRL_YUVIFMT;
            		format |= (OUT_SIZE_QCIF | IN_FMT_CCIR656_NTSC );
            		high = 176;
            		times = 176*120*BYTES/16;
#if defined( FIC8120 ) || defined (FIE8150)	
            		fcap_reset_workaround = 1;
#endif
                }
                else
                {
                    printk("Fail to set resolution!!\n");
                    return -1;
                }
                break;
            case INPUT_SONY_ICX409AK:
                printk("Not implement!\n");
                break;
            case INPUT_7113SVIDEO:
            case INPUT_7113COMPOSITE:
                if((width==720)&&((height==576)||(height==480)||(height==240)))
                {                        
                    if(height == 480)
                    {
                        GPIO_Video_In_Select(5);
                        format |= (OUT_SIZE_FULL | CTRL_YUVIFMT | IN_FMT_CCIR601_NTSC);
                    }
                    else if(height==576)
                    {
                        GPIO_Video_In_Select(5);
                        format |= (OUT_SIZE_FULL | CTRL_YUVIFMT | IN_FMT_CCIR601_PAL);
                    }
                    else if(height==240)
                    {
                        GPIO_Video_In_Select(6);
                        format |= (OUT_SIZE_FULL | IN_FMT_CCIR656_NTSC | (0x1 << 9)) ;
                    }                        
                    high = 720;
                    times = 720*height*BYTES/16;
#if defined( FIC8120 ) || defined (FIE8150)	
                    fcap_reset_workaround = 0;
#endif
                }
                else if((width == 352)&&((height == 240)||(height == 288)))
                {
                    GPIO_Video_In_Select(4);
                    format &= ~CTRL_YUVIFMT;
                    if(height == 240)
                        format |= (OUT_SIZE_CIF | IN_FMT_CCIR656_NTSC);
                    else if(height == 288)
                        format |= (OUT_SIZE_CIF | IN_FMT_CCIR656_PAL);
                    high = 352;
                    times = 352*height*BYTES/16;
#if defined( FIC8120 ) || defined (FIE8150)	
                    fcap_reset_workaround = 1;
#endif
                }
                else if((width == 176)&&((height==128)||(height==120)||(height==144)))
                {
                    GPIO_Video_In_Select(4);
                    if((height==128)||(height==120))
                    {
                        format |= (OUT_SIZE_QCIF | IN_FMT_CCIR656_NTSC);
                        height=120;
                    }
                    else if(height==144)
                        format |= (OUT_SIZE_QCIF | IN_FMT_CCIR656_PAL);

                    format &= ~CTRL_YUVIFMT;
                    format |= (OUT_SIZE_QCIF | IN_FMT_CCIR656_NTSC );
                    high = 176;
                    times = 176*height*BYTES/16;
#if defined( FIC8120 ) || defined (FIE8150)	
                    fcap_reset_workaround = 1;
#endif
                }
                break;
            case INPUT_IPTV7113COMPOSITE:
            case INPUT_IPTV7113TUNER:
            case INPUT_IPTV7113SVIDEO:
            case INPUT_IPTV7137COMPOSITE:
            case INPUT_IPTV7137TUNER:
            case INPUT_IPTV7137SVIDEO:
                if(width == 720 && height == 480)
                {
           	        GPIO_Video_In_Select(5);
        		    format |= (OUT_SIZE_FULL | CTRL_YUVIFMT | IN_FMT_CCIR601_NTSC );
        		    high = 720;
        		    times = 720*480*BYTES/16;
#if defined( FIC8120 ) || defined (FIE8150)	
        		    fcap_reset_workaround = 0;
#endif
                }
                else if(width == 352 && height == 240)
                {
           	    	GPIO_Video_In_Select(4);
            		format &= ~CTRL_YUVIFMT;
//            		format |= (OUT_SIZE_CIF | IN_FMT_CCIR656_NTSC | (0x1 << 9));
            		format |= (OUT_SIZE_CIF | IN_FMT_CCIR656_NTSC);
            		high = 720;
            		times = 352*240*BYTES/16;
#if defined( FIC8120 ) || defined (FIE8150)	
            		fcap_reset_workaround = 1;
#endif
                }
                else if((width == 176) && ((height == 128) ||(height==120)))
                {
        	    	GPIO_Video_In_Select(4);
            		format &= ~CTRL_YUVIFMT;
//            		format |= (OUT_SIZE_QCIF | IN_FMT_CCIR656_NTSC | (0x1 << 9));
            		format |= (OUT_SIZE_QCIF | IN_FMT_CCIR656_NTSC);
            		high = 720;
            		times = 176*120*BYTES/16;
#if defined( FIC8120 ) || defined (FIE8150)	
            		fcap_reset_workaround = 1;
#endif
                }
		    	else if (width == 720 && height == 576) {
	    	    	GPIO_Video_In_Select(5);
		    		format |= (OUT_SIZE_FULL | CTRL_YUVIFMT | IN_FMT_CCIR601_PAL);
		    		high = 720;
		    		times = 720*576*BYTES/16;
		    		#if defined( FIC8120 ) || defined (FIE8150)	
		    		fcap_reset_workaround = 0;
		    		#endif
		    	}
		    	else if (width == 352 && height == 288) {
	    	    	GPIO_Video_In_Select(4);
		    		format &= ~CTRL_YUVIFMT;
		    		format |= (OUT_SIZE_CIF | IN_FMT_CCIR656_PAL);
		    		high = 720;
		    		times = 352*288*BYTES/16;
		    		#if defined( FIC8120 ) || defined (FIE8150)	
		    		fcap_reset_workaround = 1;
		    		#endif
		    	}
		    	else if (width == 176 && height == 144) {
	    	    	GPIO_Video_In_Select(4);
		    		format &= ~CTRL_YUVIFMT;
		    		format |= (OUT_SIZE_QCIF | IN_FMT_CCIR656_PAL);
		    		high = 720;
		    		times = 176*144*BYTES/16;
		    		#if defined( FIC8120 ) || defined (FIE8150)	
		    		fcap_reset_workaround = 1;
		    		#endif
		    	}	
                else
                {
                    printk("Fail to set resolution!!\n");
                    return -1;
                }
                break;
            default:
                return -1;
        }
    }
	// real store into registers	
	capture->High = high;
	capture->Times = times;
		
#ifdef VIDEO_MODE_422	
	capture->Format = format | OUT_SOFMT;
#else
	capture->Format = format;
#endif			
	return 0;
}


